//
//  LoginViewController.swift
//  ParkManage
//
//  Created by cf on 2020/9/17.
//  Copyright © 2020 diligrp. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController {

    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var phoneTextField: UITextField!
    @IBOutlet weak var desPhoneLabel: UILabel!
    @IBOutlet weak var line1: UIView!
    
    @IBOutlet weak var pwdTextField: UITextField!
    @IBOutlet weak var desPwdLabel: UILabel!
    @IBOutlet weak var line2: UIView!
    
    @IBOutlet weak var accountLoginButton: UIButton!
    @IBOutlet weak var wxLoginButton: UIButton!
    @IBOutlet weak var pwdSecureButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUITheme()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    func setUITheme() {
        
        bgView.backgroundColor = UIColor.white.withAlphaComponent(0.7)
        
        bgView.makeRoundedCorners(15)
        line1.backgroundColor = .themeLineGray()
        line2.backgroundColor = .themeLineGray()
        accountLoginButton.makeRoundedCorners(22.5)
        wxLoginButton.makeRoundedCorners(22.5)
    }
    
    @IBAction func accountLogin(_ sender: UIButton) {
        Configs.Dimensions.AppWindow.rootViewController =
            BaseNavigationController(rootViewController: HomeViewController())
    }
    
    @IBAction func wxLogin(_ sender: UIButton) {
        
//        UMSocialManager.default()?.getUserInfo(with: .wechatSession,
//       currentViewController: self, completion: { (result, error) in
//            if error != nil {
//
//            } else {
//                //let  resp: UMSocialUserInfoResponse = (result as? UMSocialUserInfoResponse)!
//            }
//        })
        navigator.push(LoginURL.connectWX.path)
    }
    
    @IBAction func isDisplayPwd(_ sender: UIButton) {
        pwdTextField.isSecureTextEntry = !pwdTextField.isSecureTextEntry
        
        let image = pwdTextField.isSecureTextEntry == true ?
            UIImage(named: "icon_hiden_pwd") : UIImage(named: "icon_show_pwd")
        
        sender.setImage(image, for: .normal)
    }
}

extension LoginViewController: UITextFieldDelegate {

    func textFieldDidBeginEditing(_ textField: UITextField) {
        
        if textField == phoneTextField {
            line1.backgroundColor = .themeBlue()
            UIView.animate(withDuration: 0.25) {
                self.desPhoneLabel.alpha = 1
            }
        } else if textField == pwdTextField {
            line2.backgroundColor = .themeBlue()
            UIView.animate(withDuration: 0.25) {
                self.desPwdLabel.alpha = 1
            }
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == phoneTextField {
            line1.backgroundColor = .themeLineGray()
            if textField.text?.count == 0 {
                UIView.animate(withDuration: 0.25) {
                    self.desPhoneLabel.alpha = 0
                }
            }
            
        } else if textField == pwdTextField {
            line2.backgroundColor = .themeLineGray()
            if textField.text?.count == 0 {
                UIView.animate(withDuration: 0.25) {
                    self.desPwdLabel.alpha = 0
                }
            }
        }
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if phoneTextField == textField {
            
            if range.location >= 11 {
                return false
            }
            
        } else if pwdTextField == textField {
            if range.location >= 6 {
                return false
            }
        }
        return true
    }
}
